<template>
  <transition
    name="move"
    @after-enter="afterEnter"
    @before-leave="beforeLeave"
  >
    <div class="food" v-show="visible">
      <div class="back" @click="hide">
        <i class="icon-arrow_lift"></i>
        <div class="header-title">
           {{foodInfo.name}}
        </div>
      </div>
      <cube-scroll ref="scroll">
        <div class="image-header">
          <img :src="foodInfo.image" alt="">
        </div>
        <div class="food-title">
          <h2 class="name">{{foodInfo.name}}</h2>
          <div class="detail">
            <span class="sell-count">月售{{foodInfo.sellCount}}份</span>
            <span class="rating">好评率{{foodInfo.rating}}%</span>
          </div>
          <div class="price">
            <span class="now">￥{{foodInfo.price}}</span>
            <span class="old" v-show="foodInfo.oldPrice">￥{{foodInfo.oldPrice}}</span>
          </div>
          <div class="cart-control-wrapper">
            <cart-control :food="foodInfo"></cart-control>
          </div>
          <transition name="fade">
            <div 
              class="buy"
              v-show="!selectedFoodCount"
              @click="addFood"
            >加入购物车</div>
          </transition>
        </div>
        <split v-show="foodInfo.info"></split>
        <div class="info" v-show="foodInfo.info">
          <h1 class="title">商品信息</h1>
          <p class="text">{{foodInfo.info}}</p>
        </div>
        <split></split>
        <div class="rating">
          <h1 class="title">商品评价</h1>
          <rating-select
            :desc="desc"
            :selectType="selectType"
            :onlyContent="onlyContent"
            :ratings="ratings"
            @select="onSelect"
            @toggle="onToggle"
          >
          </rating-select>
          <div class="rating-wrapper">
            <ul v-show="computedRatings && computedRatings.length">
              <li
                v-for="(rating,index) in computedRatings"
                class="rating-item border-bottom-1px"
                :key="index"
              >
                <div class="user">
                  <span class="name">{{rating.username}}</span>
                  <img class="avatar" width="12" height="12" :src="rating.avatar">
                </div>
                <div class="time">{{format(rating.rateTime)}}</div>
                <p class="text">
                  <span :class="{'icon-thumb_up':rating.rateType===0,'icon-thumb_down':rating.rateType===1}"></span>{{rating.text}}
                </p>
              </li>
            </ul>
            <div class="no-rating" v-show="!computedRatings || !computedRatings.length">暂无评价</div>
          </div>
        </div>
      </cube-scroll>
      <!-- 为了让购物车logo能正常置顶显示 -->
      <div class="cart-sticky" v-show="ballVisible">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight': this.$store.getters.getAllFoodsCount.count>0}">
            <i class="icon-shopping_cart" :class="{'highlight': this.$store.getters.getAllFoodsCount.count>0}"></i>
          </div>
          <div class="num">
            <bubble :num="this.$store.getters.getAllFoodsCount.count"></bubble>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import moment from 'moment'
  import CartControl from 'components/cart-control/cart-control'
  import Split from 'components/split/split'
  import RatingSelect from 'components/rating-select/rating-select'
  import popupMixin from 'common/mixins/popup'
  import ratingMixin from 'common/mixins/rating'
  import Bubble from 'components/bubble/bubble'
  
  // const EVENT_SHOW = 'show'
  export default {
    name: 'food',
    mixins: [popupMixin, ratingMixin],
    props: {
      selectFood: {
        type: Object
      }
    },
    data() {
      return {
        food: this.selectFood,
        desc: {
          all: '全部',
          positive: '推荐',
          negative: '吐槽'
        },
        ballVisible: false
      }
    },
    computed: {
      selectedFoodCount() {
        return this.$store.getters.getFoodCount[this.foodInfo.name] >= 0 ? this.$store.getters.getFoodCount[this.foodInfo.name] : 0
      },
      ratings() {
        return this.foodInfo.ratings
      },
      foodInfo() {
        return this.food
      }
    },
    created() {
      this.$on('foodShow', () => {
        this.$nextTick(() => {
          this.$refs.scroll.refresh()          
        })
      })
    },
    methods: {
      foodShow(data) {
        this.visible = true
        // this.$emit(EVENT_SHOW)
        this.food = data
        this.$nextTick(() => {
          this.$refs.scroll.refresh()
        })
        // console.log(this.foodInfo)
      },
      addFood() {
          this.$set(this.foodInfo, 'count', 1)
          const foodinfo = {
          name: this.foodInfo.name,
          price: this.foodInfo.price,
          count: this.foodInfo.count
        }
        // 将购物数据存储到vuex仓库中
        this.$store.commit("addToCar", foodinfo)
      },
      afterEnter() {
        this.ballVisible = this.visible
        this.$refs.scroll.refresh() // 动画加载完毕后，调用scroll的refresh()，重新计算scroll的高度
      },
      beforeLeave() {
        this.ballVisible = this.visible
      },
      format(time) {
        return moment(time).format('YYYY-MM-DD hh:mm')
      }
    },
    components: {
      CartControl,
      Split,
      RatingSelect,
      Bubble
    }    
  }
</script>

<style lang="stylus" scoped>
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin.styl"
  .food
    position: fixed
    left: 0
    top: 0
    bottom: 48px
    z-index: 30
    width: 100%
    background: $color-white
    &.move-enter-active, &.move-leave-active
      transition: all 0.3s linear
    &.move-enter, &.move-leave-active
      transform: translate3d(100%, 0, 0)
    .back
      position absolute
      height 40px
      width 100%
      background-color #fff
      z-index 100
      .icon-arrow_lift
        position absolute
        top 0px
        left 5px
        padding 10px
        font-size $fontsize-large-xx
        color #000
      .header-title
        width 100%
        line-height 40px
        text-align center
        font-size $fontsize-large
    .image-header
      margin-top 40px
      img 
        width 100%
        height 100%
    .food-title
      padding 18px
      position relative
      .name
        margin-bottom 8px
        // line-height 14px
        // font-size $fontsize-medium
        font-weight 700
        color $color-dark-grey
      .detail
        margin-bottom 18px
        line-height 10px
        font-size $fontsize-small-s
        color $color-light-grey
        .sell-count
          margin-right 12px
      .price
        line-height 24px
        .now
          font-size 14px
          font-weight 700
          margin-right 8px
          color $color-red
        .old
          text-decoration line-through
          font-weight 700
          font-size $fontsize-small-s
          color $color-light-grey
      .cart-control-wrapper
        position absolute 
        right 18px
        bottom 14px
      .buy
        position absolute 
        right 18px
        bottom 18px
        height 24px
        padding 0 12px
        border-radius 12px
        line-height 24px
        font-size $fontsize-small-s
        color $color-white
        background-color $color-blue
        opacity 1
        &.fade-enter-active, &.fade-leave-active
          transition: all 0.3s
        &.fade-enter, &.fade-leave-active
          opacity: 0
          z-index: -1
    .info
      padding: 18px
      .title
        line-height: 14px
        margin-bottom: 6px
        font-size: $fontsize-medium
        font-weight 700
        color: $color-dark-grey
      .text
        line-height: 24px
        padding: 0 8px
        font-size: $fontsize-small
        color: $color-grey
    .rating
      // padding-top 18px
      .title
        padding 18px 18px 0 18px
        font-size: $fontsize-medium
        font-weight 700
      .rating-wrapper
        padding: 0 18px
        .rating-item
          position: relative
          padding: 16px 0
          &:last-child
            border-none()
          .user
            position: absolute
            right: 0
            top: 16px
            display: flex
            align-items: center
            line-height: 12px
            .name
              margin-right: 6px
              font-size: $fontsize-small-s
              color: $color-light-grey
            .avatar
              border-radius: 50%
          .time
            margin-bottom: 6px
            line-height: 12px
            font-size: $fontsize-small-s
            color: $color-light-grey
          .text
            line-height: 16px
            font-size: $fontsize-small
            color: $color-dark-grey
            .icon-thumb_up, .icon-thumb_down
              margin-right: 4px
              line-height: 16px
              font-size: $fontsize-small
            .icon-thumb_up
              color: $color-blue
            .icon-thumb_down
              color: $color-light-grey

        .no-rating
          padding: 16px 0
          font-size: $fontsize-small
          color: $color-light-grey
    .cart-sticky
      position absolute
      z-index 300
      .logo-wrapper
        display inline-block
        vertical-align top
        position relative
        top -10px
        left: 12px
        width: 56px
        height: 56px
        border-radius: 50%
        box-sizing: border-box
        background-color: $color-background
        .logo
          width: 44px
          height: 44px
          border-radius: 50%  
          text-align: center
          background-color: $color-dark-grey
          margin: 6px auto
          &.highlight
            background-color: $color-blue
          .icon-shopping_cart
            line-height: 44px
            font-size: $fontsize-large-xxx
            color: $color-light-grey
            &.highlight
              color: $color-white
        .num
          position absolute
          top 0
          right 0        
</style>
